package com.jckl.foodathon.restlet.services;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import com.google.gson.Gson;
import com.jckl.foodathon.dao.BlogDAO;
import com.jckl.foodathon.model.Blog;
import com.jckl.foodathon.model.Store;


/**
 * Resource which has only one representation.
 * 
 */
public class BlogsOfStoreResource extends ServerResource {
	
	private static final Logger log = Logger.getLogger(BlogsOfStoreResource.class.getName());

    @Put("json")
    public String getBlogsByStore(Representation entity) {
    	
    	log.info("Entering: getBlogsByStore");
    	
    	String returnString = "";
    	try{
    		JSONObject req = (new JsonRepresentation(entity)).getJsonObject();
    		Gson gson = new Gson();
    		
    		Store store = gson.fromJson(req.toString(), Store.class);
    		
    		BlogDAO blogDAO = new BlogDAO();
    		List<Blog> blogs = blogDAO.getBlogsByStore(store.getName());
    		
    		returnString = new Gson().toJson(blogs);

    	}catch(JSONException jsonEx){
    		jsonEx.printStackTrace();
    	}catch(IOException ioEx){
    		ioEx.printStackTrace();
    	}
		return returnString;
    }
    
    @Get
    public String testBlogsOfStore(){
    	log.info("Entering: testBlogsOfStore");
    	
    	
    	String returnString = "";
		Store store = new Store();
		store.setName("Soya Sauce Chicken At Jalan Ipoh");
		
		BlogDAO blogDAO = new BlogDAO();
		List<Blog> blogs = blogDAO.getBlogsByStore(store.getName());
		
		returnString = new Gson().toJson(blogs);
		return returnString;
    }
    
}
